package com.jinzhi.jzweb.service.impl;

import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.domain.Tree;
import com.jinzhi.common.utils.BuildTree;
import com.jinzhi.jzweb.dao.KnowledgePointsDao;
import com.jinzhi.jzweb.domain.CertificateNewsDO;
import com.jinzhi.jzweb.domain.KnowledgePointsDO;
import com.jinzhi.jzweb.domain.TestPaperTemplateDO;
import com.jinzhi.jzweb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 
 * <pre>
 * 知识点
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Service
public class KnowledgePointsServiceImpl extends CoreServiceImpl<KnowledgePointsDao, KnowledgePointsDO> implements KnowledgePointsService {

    @Autowired
    private ItemBankTkService itemBankTkService;
    @Autowired
    private TestPaperTemplateService testPaperTemplateService;
    @Autowired
    private KnowledgePointsService knowledgePointsService;
    @Autowired
    private CertificateNewsService certificateNewsService;
    @Autowired
    private ItemBankService itemBankService;

    @Override
    public Tree<KnowledgePointsDO> getTree() {
        List<Tree<KnowledgePointsDO>> trees = new ArrayList<Tree<KnowledgePointsDO>>();
        List<KnowledgePointsDO> sysDepts = baseMapper.selectList(null);
        for (KnowledgePointsDO sysDept : sysDepts) {
            Tree<KnowledgePointsDO> tree = new Tree<KnowledgePointsDO>();
            tree.setLevel(sysDept.getLevel());
            tree.setId(sysDept.getId().toString());
            tree.setParentId(sysDept.getPid().toString());
            tree.setText(sysDept.getName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<KnowledgePointsDO> t = BuildTree.build(trees);
        return t;
    }

    @Override
    public List<Map<Object,Object>> selByAll(){
        return baseMapper.selByAll();
    }

    @Override
    public List<KnowledgePointsDO> selectListBy(Map map) {
        return baseMapper.selectListBy(map);
    }

    @Override
    public List<KnowledgePointsDO> selectListAll() {
        return baseMapper.selectListAll();
    }

    @Override
    public List<KnowledgePointsDO> selByProidGrade(Map map){
        return baseMapper.selByProidGrade(map);
    }

    @Override
    public List<KnowledgePointsDO> knowss(String tptemplateId) {

        List<KnowledgePointsDO> knowledgePointsDO = new ArrayList<>();
        TestPaperTemplateDO testPaperTemplateDO = testPaperTemplateService.selectById(tptemplateId);
        Map map1 = new HashMap();
        if (testPaperTemplateDO != null) {
            CertificateNewsDO certificateNewsDO = certificateNewsService.selectById(testPaperTemplateDO.getNewsId());
            if (certificateNewsDO != null) {

                map1.put("profileId", certificateNewsDO.getProfileId());
                if (certificateNewsDO.getGrade() == 0) {
                    map1.put("grade", "高级");
                } else if (certificateNewsDO.getGrade() == 1) {
                    map1.put("grade", "中级");
                } else if (certificateNewsDO.getGrade() == 2) {
                    map1.put("grade", "初级");
                }
                knowledgePointsDO = knowledgePointsService.selByProidGrade(map1);
            }
        }


        for (int i = 0; i < knowledgePointsDO.size(); i++) {
            Map selectItemMap = new HashMap();
            selectItemMap.put("kpoints_id", knowledgePointsDO.get(i).getId());
            selectItemMap.put("grade", map1.get("grade"));
            List<Map<Object, Object>> itemNum = null;
            if (testPaperTemplateDO.getName().indexOf("泰康") != -1) {
                itemNum = itemBankTkService.selNumByKpoints(selectItemMap);
            } else {
                itemNum = itemBankService.selNumByKpoints(selectItemMap);
            }


            for (Map map : itemNum) {
                if (map.get("itemBankType").equals(0)) {
                    if (map.get("topicTypes").equals(0)) {
                        knowledgePointsDO.get(i).setSingleNum0(Integer.parseInt(map.get("num").toString()));
                    } else if (map.get("topicTypes").equals(1)) {
                        knowledgePointsDO.get(i).setMultipleNum0(Integer.parseInt(map.get("num").toString()));
                    } else {
                        knowledgePointsDO.get(i).setJudgeNum0(Integer.parseInt(map.get("num").toString()));
                    }
                } else {
                    if (map.get("topicTypes").equals(0)) {
                        knowledgePointsDO.get(i).setSingleNum1(Integer.parseInt(map.get("num").toString()));
                    } else if (map.get("topicTypes").equals(1)) {
                        knowledgePointsDO.get(i).setMultipleNum1(Integer.parseInt(map.get("num").toString()));
                    } else {
                        knowledgePointsDO.get(i).setJudgeNum1(Integer.parseInt(map.get("num").toString()));
                    }
                }

            }
        }
        return knowledgePointsDO;
    }
}
